package com.fufan.mtsaas.modules.sysmgr.syuser.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.fufan.mtsaas.modules.common.bo.request.DataRequest;
import com.fufan.mtsaas.modules.common.bo.request.DataRequestT;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.sygroup.model.SYgroupH;
import com.fufan.mtsaas.modules.sysmgr.syuser.bo.reponse.RoleSelectResponse;
import com.fufan.mtsaas.modules.sysmgr.syuser.bo.request.*;
import com.fufan.mtsaas.modules.sysmgr.syuser.dao.SYuserDao;
import com.fufan.mtsaas.modules.sysmgr.syuser.model.SYuser;
import com.fufan.mtsaas.modules.sysmgr.syuser.bo.response.UserResponse;
import com.fufan.mtsaas.modules.sysmgr.syuser.service.SYuserSvc;
import com.fufan.mtsaas.tools.AESTool;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 
 * </p>
 *
 * @author Liby
 * @since 2019-08-14
 */
@Slf4j
@Api(tags = "MTSaas：系统管理-用户信息模块", description = "SYuser对象接口")
@RestController
@RequestMapping("/SYS/SYuser")
public class SYuserCtrl {
    @Autowired
    private SYuserSvc targetService;
    @Autowired
    private SYuserDao sYuserDao;

    /**
     * 分页查询
     * */
    @ApiOperation(value = "分页查询所有，根据需要传入分页参数：pageSize每页数量(默认为10)，currentPage当前页数(默认为1)", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/doQueryPage")
    public Result<UserResponse> doQueryPage(@RequestBody @ApiIgnore DataRequest dataRequest, UserQueryRequest request){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        JSONObject mapdata = dataRequest.getData().getParam();
        UserQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), UserQueryRequest.class);

        return resultPack.doResultPack(targetService.doQueryPage(map),requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 修改一个用户关联多个角色
     */
    @ApiOperation(value = "修改一个用户关联多个角色", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doSaveRole", method = RequestMethod.POST)
    public Result<SYgroupH> doSaveRole(@RequestBody @ApiIgnore @Valid DataRequestT<UserGroupSaveRequest> dataRequest, UserGroupSaveRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doSaveRole(dataRequest.getData().getParam(), requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 修改一个角色关联多个用户
     */
    @ApiOperation(value = "修改一个角色关联多个用户", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doSaveGroup", method = RequestMethod.POST)
    public Result<SYuser> doSaveGroup(@RequestBody @ApiIgnore @Valid DataRequestT<GroupUserSaveRequest> dataRequest, GroupUserSaveRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doSaveGroup(dataRequest.getData().getParam(), requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }


    /**
     * 查询用户用于的角色列表
     */
    @ApiOperation(value = "查询用户用于的角色列表", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doQueryUserRole", method = RequestMethod.POST)
    public Result<SYgroupH> doQueryUserRole(@RequestBody @ApiIgnore @Valid DataRequestT<UserGroupQueryRequest> dataRequest, UserGroupQueryRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doQueryUserRole(dataRequest.getData().getParam(), requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 查询角色对应的用户列表
     */
    @ApiOperation(value = "查询角色对应的用户列表", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doQueryRoleUser", method = RequestMethod.POST)
    public Result<SYuser> doQueryRoleUser(@RequestBody @ApiIgnore @Valid DataRequestT<UserGroupQueryRequest> dataRequest, UserGroupQueryRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doQueryRoleUser(dataRequest.getData().getParam(), requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 新增
     * */
    @ApiOperation(value = "新增", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    public Result<SYuser> doSaveMap(@RequestBody @ApiIgnore @Valid DataRequestT<UserSaveRequest> dataRequest, UserSaveRequest request, @ApiIgnore HttpSession session){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doSaveMap(dataRequest.getData().getParam(), requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 修改
     * */
    @ApiOperation(value = "修改", httpMethod = "PUT", produces = "application/json")
    @RequestMapping(value = "/doUpdate", method = RequestMethod.PUT)
    public Result<SYuser> doUpdateMap(@RequestBody @ApiIgnore @Valid DataRequestT<UserSaveRequest> dataRequest, UserSaveRequest request, @ApiIgnore HttpSession session){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        return resultPack.doResultPack(targetService.doUpdateMap(dataRequest.getData().getParam(), requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 删除数据
     * */
    @ApiOperation(value = "删除数据", httpMethod = "DELETE", produces = "application/json")
    @RequestMapping(value = "/doDelete", method = RequestMethod.DELETE)
    public Result doDelete(@RequestBody @ApiIgnore DataRequest dataRequest, UserQueryRequest request){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        UserQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), UserQueryRequest.class);

        return resultPack.doResultPack(targetService.doDelete(map,requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
    }



    /**
     * 启用
     * */
    @ApiOperation(value = "启用", httpMethod = "PUT", produces = "application/json")
    @RequestMapping(value = "/doOpen", method = RequestMethod.PUT)
    public Result<SYuser> doOpen(@RequestBody @ApiIgnore DataRequest dataRequest, UserModifyRequest request, @ApiIgnore HttpSession session){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        UserModifyRequest map = JSonUtil.toBean(mapdata.toJSONString(), UserModifyRequest.class);
        SYuser sYuser = new SYuser();
        sYuser.setUserID(map.getUserID());
        sYuser.setVldFG("0");
//        sYuser.setCompanyID(requestPojo.getCustId());
//        sYuser.setId(map.getId());

        Map<String,String> userMap = new HashMap<>();
        try {
            userMap.put("UserID", AESTool.encrypt(map.getUserID()));
        } catch (Exception e) {
        }
        List<SYuser> sYuserList = sYuserDao.selectList(new Page<>(),userMap);
        sYuser.setId(sYuserList.get(0).getId());
        return resultPack.doResultPack(targetService.doUpdate(sYuser, requestPojo, null), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 停用
     * */
    @ApiOperation(value = "停用", httpMethod = "PUT", produces = "application/json")
    @RequestMapping(value = "/doOff", method = RequestMethod.PUT)
    public Result<SYuser> doOff(@RequestBody @ApiIgnore DataRequest dataRequest, UserModifyRequest request, @ApiIgnore HttpSession session){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        UserModifyRequest map = JSonUtil.toBean(mapdata.toJSONString(), UserModifyRequest.class);
        SYuser sYuser = new SYuser();
        sYuser.setUserID(map.getUserID());
        sYuser.setVldFG("1");
//        sYuser.setId(map.getId());

        Map<String,String> userMap = new HashMap<>();
//        userMap.put("id",map.getId() + "");
        try {
            userMap.put("UserID", AESTool.encrypt(map.getUserID()));
        } catch (Exception e) {
        }
        List<SYuser> sYuserList = sYuserDao.selectList(new Page<>(),userMap);
        sYuser.setId(sYuserList.get(0).getId());
//        sYuser.setCompanyID(requestPojo.getCustId());
        return resultPack.doResultPack(targetService.doUpdate(sYuser, requestPojo, null), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 用户添加角色下拉接口
     * */
    @ApiOperation(value = "查询公司下所有角色 下拉接口", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/allRole/doQueryPage")
    public Result<RoleSelectResponse> doQueryAllRole(){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        return resultPack.doResultPack(targetService.doQueryAllRole(requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改密码", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doUpdatePass", method = RequestMethod.POST)
    public Result doUpdatPass(@RequestBody @ApiIgnore DataRequest dataRequest, UserResetPassRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        JSONObject mapdata = dataRequest.getData().getParam();
        UserResetPassRequest map = JSonUtil.toBean(mapdata.toJSONString(), UserResetPassRequest.class);

        return resultPack.doResultPack(targetService.doUpdatePass(map, requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

//    /**
//     * 重置密码
//     * Liby 2019-12-16
//     * */
//    @ApiOperation(value = "重置密码", httpMethod = "POST", produces = "application/json")
//    @ApiResponses({
//            @ApiResponse(code = 4001, message = "参数错误"),
//            @ApiResponse(code = 3002, message = "已存在"),
//            @ApiResponse(code = 3003, message = "修改出错")
//    })
////    @RequestMapping(value = "/doResetPass", method = RequestMethod.POST)
//    public Result doResetPass(@RequestBody String map, @ApiIgnore HttpSession session){
//        ResultPack resultPack = new ResultPack();
//        RequestPojo requestPojo = ParamUtil.doChange();
//        return resultPack.doResultPack(targetService.doResetPass(map,requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
//    }

    /**
     * 分页查询
     * */
    @ApiOperation(value = "根据部门id查询所有科室下所有人", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/getUserList")
    public Result<UserResponse> getUserList(@RequestBody DataRequestT<UserQueryRequest> dataRequest){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        UserQueryRequest map = dataRequest.getData().getParam();

        return resultPack.doResultPack(targetService.getUserList(map),requestPojo.getIntfId(),requestPojo.getAppKey());
    }
}
